package com.kaifamiao.dao.impl;

import com.kaifamiao.dao.CustomerDAO;
import com.kaifamiao.entity.Customer;
import org.springframework.orm.hibernate5.HibernateTemplate;

import java.util.List;

public class CustomerDaoByHibernate implements CustomerDAO {

    private HibernateTemplate template;

    @Override
    public Customer find(Integer id) {
        Customer c = template.get( Customer.class, id );
        return c;
    }

    @Override
    public List<Customer> findAll() {
        List<Customer> list = template.loadAll( Customer.class );
        return list;
    }

    @Override
    public int persist(Customer c) {
        Integer id = (Integer)template.save(c);
        return id;
    }

    @Override
    public int update(Customer c) {
        template.update( c );
        return 1;
    }

    @Override
    public int remove(Integer id) {
        Customer entity = template.get( Customer.class, id );
        template.delete( entity );
        return 1;
    }

    public HibernateTemplate getTemplate() {
        return template;
    }

    public void setTemplate(HibernateTemplate template) {
        this.template = template;
    }

}
